<?php

namespace lanchain;

use think\facade\Config;
use think\facade\Log;

class LanchainServer
{
    // 模拟测试 验证成功
    protected $allow_host = [
        'http://web.spreadcard.io',
        'http://spread.test.jxqianbao.com'
    ];
    /**
     * @param $data 开户
     */
    public function openAccount($data)
    {
        if(in_array(env('host'), $this->allow_host)) {
            return [
                'code' => '0000',
                'response' => [
                    'mercId' => '1234567'
                ]
            ];
        }
        $host = Config::get('lanchain.api_host');
        $path = '/csh_orgs/merc/open';
        $param = [
            'agentId' => Config::get('lanchain.agent_id'),
            'encryptData' => $this->encryptData($data),
            'signature' => $this->getSign($data)
        ];
        $post = urldecode(http_build_query($param));
        return LanchainHttp::PostCURL($host.$path, $post);
    }

    public function getBank($cityName, $tag)
    {
        $host = Config::get('lanchain.api_host');
        $path = '/csh_apps/bankCard/getBankB0800';
        $data = [
            'city' => $cityName,
            'tag' => $tag
        ];
//        $param = [
//            'agentId' => Config::get('lanchain.agent_id'),
//            'encryptData' => $this->encryptData($data),
//            'signature' => $this->getSign($data)
//        ];
        $post = urldecode(http_build_query($data));
        return LanchainHttp::PostCURL($host.$path, $post);
    }

    public function getSign($data)
    {
        return strtoupper(md5(json_encode($data ,JSON_UNESCAPED_UNICODE).Config::get('lanchain.secret_id')));
    }

    // 代扣
    public function payDeduct($data)
    {
        if(in_array(env('host'), $this->allow_host)) {
            return [
                'code' => 'A1400',
                'response' => [
                    'orderId' => 'ceshi_1234567'
                ]
            ];
        }
        $host = Config::get('lanchain.api_host');
        $path = '/csh_orgs/payFor/deduct';
        $data['businessCode'] = Config::get('lanchain.businessCode')['plan'];
        $data['notifyUrl'] = Config::get('lanchain.deduct_notify_url');
        $param = [
            'agentId' => Config::get('lanchain.agent_id'),
            'encryptData' => $this->encryptData($data),
            'signature' => $this->getSign($data)
        ];
        $post = urldecode(http_build_query($param));
        return LanchainHttp::PostCURL($host.$path, $post);
    }

    // 代付
    public function pay($data)
    {
        if(in_array(env('host'), $this->allow_host)) {
            return [
                'code' => 'A1400',
                'response' => [
                    'orderId' => '123456'
                ]
            ];
        }
        $host = Config::get('lanchain.api_host');
        $path = '/csh_orgs/payFor/pay';
        $data['notifyUrl'] = Config::get('lanchain.pay_notify_url');
        $param = [
            'agentId' => Config::get('lanchain.agent_id'),
            'encryptData' => $this->encryptData($data),
            'signature' => $this->getSign($data)
        ];
        $post = urldecode(http_build_query($param));
        return LanchainHttp::PostCURL($host.$path, $post);
    }

    // 查询
    public function query($mercOrderId)
    {
        if(in_array(env('host'), $this->allow_host)) {
            return [
                'code' => '0000',
                'response' => [
                    'orderStatus' => '2'
                ]
            ];
        }
        $host = Config::get('lanchain.api_host');
        $path = '/csh_orgs/order/query';
        $data['mercOrderId'] = $mercOrderId;
        $param = [
            'agentId' => Config::get('lanchain.agent_id'),
            'encryptData' => $this->encryptData($data),
            'signature' => $this->getSign($data)
        ];
        $post = urldecode(http_build_query($param));
        return LanchainHttp::PostCURL($host.$path, $post);
    }

    // 查询可用渠道
    public function queryChannel($mercId, $accountNo)
    {
        if(in_array(env('host'), $this->allow_host)) {
            return [
                'code'     => '0000',
                'msg'      => '',
                'response' => [
                    'channelList' => [
                        0 => [
                            'channelId'   => '100134',
                            'channelName' => '智能代还通道A',
                            'status'      => '0',
                        ]
                    ]
                ]
            ];
        }
        $host = Config::get('lanchain.api_host');
        $path = '/csh_orgs/payFor/queryChannel';
        $data = [
            'mercId' => $mercId,
            'accountNo' => $accountNo
        ];
        $param = [
            'agentId' => Config::get('lanchain.agent_id'),
            'encryptData' => $this->encryptData($data),
            'signature' => $this->getSign($data)
        ];
        $post = urldecode(http_build_query($param));
        return LanchainHttp::PostCURL($host.$path, $post);
    }

    // 验证码发送
    public function activeSms($data)
    {
        if(in_array(env('host'), $this->allow_host)) {
            sleep(5);
            return [
                'code'     => '0000',
                'msg'      => '',
            ];
        }
        $host = Config::get('lanchain.api_host');
        $path = '/csh_orgs/payFor/activeSms';
        $param = [
            'agentId' => Config::get('lanchain.agent_id'),
            'encryptData' => $this->encryptData($data),
            'signature' => $this->getSign($data)
        ];
        Log::record('===========代还验证码=============');
        Log::record($data);
        $post = urldecode(http_build_query($param));
        return LanchainHttp::PostCURL($host.$path, $post);
    }

    public function confirmSms($data)
    {
//        if(in_array(env('host'), $this->allow_host)) {
//            return [
//                'code'     => '0000',
//                'msg'      => '',
//            ];
//        }
        $host = Config::get('lanchain.api_host');
        $path = '/csh_orgs/payFor/activeConfirm';
        $param = [
            'agentId' => Config::get('lanchain.agent_id'),
            'encryptData' => $this->encryptData($data),
            'signature' => $this->getSign($data)
        ];
        Log::record('===========代还验证码确认=============');
        Log::record($data);
        $post = urldecode(http_build_query($param));
        return LanchainHttp::PostCURL($host.$path, $post);
    }



    /**
     * @param $data base64Data
     *
     * @return bool|string
     */
    public function encryptData($data)
    {
        if(!$data) {
            return false;
        }
        return base64_encode(json_encode($data, JSON_UNESCAPED_UNICODE));
    }

}